##########################################
  
  #Name: install.sh
  #Desc: install docker-compose
  #Author: OracleGao
  #Email: oraclegao@126.com
  #Create Date: 2020-06-29
  #Git: https://gitee.com/docker-helper/docker-compose-bin
  #Origin: https://gitee.com/docker-helper/docker-compose-bin/raw/master/install.sh

##########################################

#!/usr/bin/env bash
set -e

current_path=$(pwd)

_script_name_="install.sh"

tmp_path="/tmp/dcb$(date "+%Y%m%d%H%M%S")"
version_default=1.26.0
versions=(1.26.0)
kernel_name=$(uname -s)
machine=$(uname -m)
compose_file_default="/usr/bin/docker-compose"
set +e
installed_compose_file=$(which docker-compose)
set -e

function empty_error() {
  echo "ERROR:"
  echo "  $1 parameter can not be empty"
  echo ""
  echo "Usage:"
  echo "  ${_script_name_} -h | --help"
  echo ""
  exit 2
}

function check_version() {
  local _version
  for _version in ${versions[*]}; do
    if [ "${_version}" == $1 ]; then
      return 0
    fi
  done
  echo "ERROR:"
  echo "  unsupport version [$1]"
  echo ""
  version_usage
  exit 3
}

function version_usage() {
  echo "VERSIONS:"
  local _version
  for _version in ${versions[*]}; do
    if [ "${_version}" == "${version_default}" ]; then
      echo "  ${_version} (default)"
    else
      echo "  ${_version}"
    fi
  done
  echo ""
}

function usage() {
  if [ "$1" == "help" ]; then
    echo "HELP:"
    echo "  install docker-compose"
    echo ""
  fi
  echo "Usage:"
  echo "  ${_script_name_} [OPTIONS]"
  echo "  ${_script_name_} -h | --help"
  echo ""
  echo "OPTIONS:"
  echo "  -D, --debug                    debug mode, show debug message"
  echo "  -h, --help                     show help and exit"
  echo "  -f, --force                    force install docker-compose, the existed bin file will be deleted"
  echo "  -V, --bin-version <VERSION>    the version of the docker-compose bin file will be installed, (default: ${version_default})"
  echo ""
  version_usage
}

debug_flag=0
force_flag=0
help_flag=0
list_version_flag=0
for arg in "$@"; do
  if [ "$1" == "" ]; then
    break
  fi
  case $1 in
    -D|--debug)
      debug_flag=1
      shift
      ;;
    -f|--force)
      force_flag=1
      shift
      ;;
    -h|--help)
      help_flag=1
      shift
      ;;
    -V|--bin-version)
      if [ $# -lt 2 -o "${2:0:1}" == "-" ]; then
        empty_error $1
      fi	    
      bin_version=$2
      shift
      shift
      ;;
    *)
      args+=("$1")
      shift
      ;;
  esac
done

if [ "${debug_flag}" == "1" ] ;then
  PS4='+[${LINENO}]>> '
  set -x
fi

if [ "${help_flag}" == "1" ]; then
  usage help
  exit 0
fi

if [ "${kernel_name}" != "Linux" ]; then
  echo "ERROR:"
  echo "  unsupport kernel name [${kernel_name}]"
  echo ""
  exit 3
fi

if [ "${machine}" != "x86_64" ]; then
  echo "ERROR:"
  echo "  unsupport machine [${machine}]"
  echo ""
  exit 3
fi

if [ "${force_flag}" == "0" ]; then
  if [ "${installed_compose_file}" != "" ]; then
    echo "ERROR:"
    echo "  docker-compose has been installed."
    echo "  you can install the new docker-compose with -f | --force parameter forcely"
    echo ""
    exit 3
  fi
fi

if [ "${version}" == "" ]; then
  _version=${version_default}
else
  check_version ${version}
  _version=${version}
fi

mkdir -p ${tmp_path}
_url_prefix="https://gitee.com/docker-helper/docker-compose-bin/raw/master/${kernel_name}/${machine}/${_version}/"
_compose_url_prefix="${_url_prefix}/docker-compose"
_compose_file="${tmp_path}/docker-compose"
_split_file_url="${_compose_url_prefix}.split"
_split_file="${_compose_file}.split"
_sha256_file_url="${_compose_url_prefix}-${kernel_name}-${machine}.sha256"
_sha256_file="${_compose_file}.sha256"
echo "preparing..."
curl -L -s ${_split_file_url}  > ${_split_file}
curl -L -s ${_sha256_file_url} > ${_sha256_file}
echo "fragment files downloading..."
for _fragment_file in $(cat ${_split_file}); do
  curl -L -s "${_url_prefix}/${_fragment_file}" > "${tmp_path}/${_fragment_file}"
  echo "download [${_fragment_file}] success"
done

cd ${tmp_path}
cat $(cat ${_split_file}) > ${_compose_file}
cd ${current_path}
sha256_origin=$(cat ${_sha256_file})
sha256_target=$(sha256sum ${_compose_file})

if [ "${sha256_origin%% *}" != "${sha256_target%% *}" ]; then
  echo "ERROR:"
  echo "  fragment files download failed, please retry."
  echo ""
  exit 4
fi

if [ "${installed_compose_file}" != "" ]; then
  echo "remove exists docker-compose file [${installed_compose_file}]"
  chmod -x ${installed_compose_file}
  mv ${installed_compose_file} ${tmp_path}/docker-compose.bak
fi
cp ${_compose_file} ${compose_file_default}
chmod +x ${compose_file_default}

_result=$(docker-compose -v)
echo "install success, ${_result}"
